#include "sysclient.h"
#include "ui_sysclient.h"
#include <main.h>
sysclient::sysclient(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::sysclient)
{
    ui->setupUi(this);
    QScreen *screen = QGuiApplication::primaryScreen();
    QRect screenGeometry = screen->geometry();
    int screenWidth = screenGeometry.width();
    int screenHeight = screenGeometry.height();

    // 设置窗口的大小为屏幕宽度和高度的60%
    int windowWidth = screenWidth * 0.6;
    int windowHeight = screenHeight * 0.6;
    setGeometry((screenWidth - windowWidth) / 2, (screenHeight - windowHeight) / 2, windowWidth, windowHeight);
//            ui->sendbtn->hide();
        setWindowFlag(Qt::FramelessWindowHint);


        // 设置窗口透明度
        setWindowOpacity(0.9);

        // 设置窗口样式表
//        setStyleSheet("background-color: rgba(0, 0, 0, 128); border-radius: 10px; color:rgb(255,255,255);");
//        setStyleSheet("background-color: rgba(128, 128, 128, 128); border-radius: 10px; color:rgb(0,0,0);");
        setStyleSheet("background-color: rgba(220,220,220, 128); border-radius: 10px; color:rgb(0,0,0);");
        ui->sendbtn->setDisabled(false);
        ui->sendbtn->setCursor(Qt::ForbiddenCursor);
}

sysclient::~sysclient()
{
    delete ui;
}

void sysclient::on_pushButtonconnect_clicked()
{

    QString host_addr = ui->lineEditaddr->text();
    quint16 host_port = ui->lineEdit_2port->text().toUShort();
    if(host_addr.isEmpty()){
        QMessageBox::information(this,
        tr("无人超市水果采集系统"
           ),
        tr("未输入端口号\n")
        );
        return ;
    }

    if(ui->pushButtonconnect->text() == "连接")
    {

        tcpSocket = new QTcpSocket(this);
        tcpSocket->connectToHost(host_addr,host_port);
        bool state = tcpSocket->waitForConnected(2000);// 2s 等待连接
        if(state){
                qDebug()<<"正在TCP Client模式...";
                connect(tcpSocket
                        ,&QTcpSocket::connected
                        ,this,&sysclient::Server_connectSucceed);
                connect(tcpSocket
                        ,&QTcpSocket::readyRead
                        ,this
                        ,&sysclient::read_ServerData);
        }
        ui->pushButtonconnect->setText("断开");
        ui->sendbtn->setEnabled(true);
        //        ui->sendbtn->hide();

        ui->sendbtn->setCursor(Qt::PointingHandCursor);
    }else {

        tcpSocket ->disconnectFromHost();
        tcpSocket = Q_NULLPTR;
        ui->pushButtonconnect->setText("连接");

//        ui->sendbtn->show();
        ui->sendbtn->setDisabled(true);

        ui->sendbtn->setCursor(Qt::ForbiddenCursor);
    }

}

//void sysclient::Server_connectSucceed(){
//    QMessageBox messageBox(this);
//}

void sysclient::Server_connectSucceed(){


}

void sysclient::read_ServerData(){

}

void sysclient::on_sendbtn_clicked()
{

    if(ui->pushButtonconnect->text() == "连接"){// 可以证明是连接中
        qDebug()<<"error";
        QMessageBox::critical(this,
        tr("客户端未连接"
           ),
        tr("禁止发送\n")
        );
        return ;
    }
        QString data  = ui->textEdit->toPlainText();
        tcpSocket -> write(data.toLocal8Bit());

}


void sysclient::mousePressEvent(QMouseEvent *event)
{
    m_dragging = true;
    m_lastPos = event->globalPos() - frameGeometry().topLeft();
}

void sysclient::mouseMoveEvent(QMouseEvent *event)
{
    if (m_dragging)
    {
        QPoint newPos = event->globalPos() - m_lastPos;
        move(newPos);
    }
}

void sysclient::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event)
    m_dragging = false;
}

void sysclient::on_exitbtn_clicked()
{
    QMessageBox::information(this,
    tr("无人超市水果采集系统"
       ),
    tr("欢迎下次使用\n""再见...\n")
    );

}
